/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.view.employment;

import com.xtd.model.employment.DepartmentPost;
import com.xtd.model.employment.Employee;
import java.util.HashMap;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Lu Yuanbin
 */
@FacesConverter(forClass = DepartmentPost.class)
public class DepartmentPostConverter implements Converter {

    private static Map<Integer, DepartmentPost> cache = new HashMap<Integer, DepartmentPost>();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value.trim().equals("")) {
            return null;
        } else {
            return cache.get(Integer.valueOf(value));
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
       
        if (value == null) {
            return null;
        } else {
            DepartmentPost departmentPost = (DepartmentPost) value;
            cache.put(departmentPost.getId(), departmentPost);
            return String.valueOf(departmentPost.getId());
        }
    }
}
